layui.use(function() {
	var table = layui.table;

	var user=layui.data("user");
	var token;
	if(user&&user.data){
		token=user.data.token;
	}
	
	table.set({
		request: {
			pageName: "pageIndex",
			limitName: "pageSize"
		},
		height: 'full-100',
		method: 'POST',
		headers: {
			Authorization: token
		},
		contentType: 'application/json',
		response: {
			statusCode: 200
		},
		parseData: function(res) {
			return {
				"code": res.code,
				"msg": res.message,
				"count": res.total,
				"data": res.data
			};
		},
		limits: [8, 16, 32, 64, 128, 256],
		cellMinWidth: 120,
		totalRow: false,
		page: true,
		elem: '#table-main',
		toolbar: '#table-toolbar'
	});
	
	table.on('edit(table-main)', function(obj){
		var params={};
		params[obj.field]=obj.value;
		params["id"]=obj.data.id;
		success=function(result){
			layer.alert("更新成功", {
				icon : 6
			});
		}
		apiput(obj.config.custom.editUri,params,success);
	});

	table.on('toolbar(table-main)', function(obj) {
		alert('请实现toolbar方法:'+obj.event);
	});
	
	table.on('tool(table-main)', function(obj){
		var data = obj.data;
		alert('请实现tool方法:'+obj.event+" id:"+data.id);
	});

});

function selectIds(table,obj){
	var id = obj.config.id;
	var checkStatus = table.checkStatus(id);
	var data = checkStatus.data;
	var ids = [];
	for (var o in data) {
		ids.push(data[o].id);
	}
	return ids;
}

function tableForDelete(table,obj,url){
	var ids = selectIds(table,obj);
	if (ids.length > 0) {
		layer.confirm('已选择:' + ids.length + '条记录,确认执行删除吗？', function(index) {
			success=function(result){
				table.reloadData('table-main');
				layer.close(index);
			}
			apidelete(url,ids,success);
		});
	} else {
		layer.alert("未选择任何数据行", {
			icon: 6
		});
	}
}
